/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nct.model;

import org.nct.model.entities.Tacgia;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.nct.model.entities.Tintuc;

/**
 *
 * @author haott
 */
public class tacGiaProvider implements IModel {

    static final Logger logger = Logger.getLogger(tacGiaProvider.class.toString());

    @Override
    // Get List Tac gia, trong moi tac gia thi co tat ca cac tin cua TAC GIA
    public List<Object> SelectAll() {
        List<Object> lists;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        logger.info("Select all");

        lists = new ArrayList<>();
        String query = "SELECT * FROM TACGIA";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);
        try {
            while (res.next()) {
                Tacgia _tg = new Tacgia(
                        res.getInt("ID"),
                        res.getString("HOTEN"),
                        res.getInt("TUOI"),
                        res.getString("BUTDANH"),
                        res.getBoolean("GIOITINH"),
                        res.getString("DIACHI"),
                        res.getString("EMAIL"),
                        res.getString("DIENTHOAI"));

                // get Tintin info
                List<Tintuc> _lstTT = new tinTucProvider().GetListTinTucFromTacGia(_tg);
                _tg.setTintucList(_lstTT);

                lists.add(_tg);
            }
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lists;
    }

    @Override
    public Object SelectOne(Object id) {

        Tacgia result = null;

        PreparedStatement selectPST = null;

        String query = "SELECT * FROM TACGIA WHERE ID = ?";
        try {
            selectPST = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            selectPST.setInt(1, Integer.parseInt(id.toString()));
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        ResultSet res = DataProvider.getInstance().ExecuteReaderPreparedStatement();
        try {

            while (res.next()) {
                result = new Tacgia(
                        res.getInt("ID"),
                        res.getString("HOTEN"),
                        res.getInt("TUOI"),
                        res.getString("BUTDANH"),
                        res.getBoolean("GIOITINH"),
                        res.getString("DIACHI"),
                        res.getString("EMAIL"),
                        res.getString("DIENTHOAI"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    @Override
    public int Insert(Object obj) {

        int count;
        PreparedStatement statement = null;

        String query = "INSERT TACGIA(ID,HOTEN,TUOI,BUTDANH,GIOITINH,DIACHI,EMAIL,DIENTHOAI)"
                + " VALUES(?,?,?,?,?,?,?,?)";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            Tacgia _tg = (Tacgia) obj;
            statement.setInt(1, _tg.getId());
            statement.setString(2, _tg.getHoten());
            statement.setInt(3, _tg.getTuoi());
            statement.setString(4, _tg.getButdanh());
            statement.setBoolean(5, _tg.getGioitinh());
            statement.setString(6, _tg.getDiachi());
            statement.setString(7, _tg.getEmail());
            statement.setString(8, _tg.getDienthoai());

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    @Override
    public int Update(Object id, Object obj) {

        int count;
        PreparedStatement statement = null;

        logger.info("Update database");

        String query = "UPDATE TACGIA SET ID=?,HOTEN=?,TUOI=?,BUTDANH=?,"
                + "GIOITINH=?,DIACHI=?,EMAIL=?,DIENTHOAI=? WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {

            Tacgia _tg = (Tacgia) obj;
            statement.setInt(1, _tg.getId());
            statement.setString(2, _tg.getHoten());
            statement.setInt(3, _tg.getTuoi());
            statement.setString(4, _tg.getButdanh());
            statement.setBoolean(5, _tg.getGioitinh());
            statement.setString(6, _tg.getDiachi());
            statement.setString(7, _tg.getEmail());
            statement.setString(8, _tg.getDienthoai());

            statement.setInt(9, Integer.parseInt(id.toString()));

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;
    }

    @Override
    public int Delete(Object id) {
        int count;
        PreparedStatement statement = null;

        logger.info("delete obj");

        String query = "DELETE FROM TACGIA WHERE ID=?";

        try {
            statement = DataProvider.getInstance().OpenWithParamater(query);

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

        try {
            statement.setInt(1, Integer.parseInt(id.toString()));

        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        count = DataProvider.getInstance().ExecuteNonQueryPreparedStatement();

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return count;

    }

    @Override
    public int increase(Object obj) {
        // change obj.ID to Max+1         
        ((Tacgia) obj).setId(getMaxID() + 1);

        return Insert(obj);
    }

    @Override
    public int getMaxID() {
        int maxID = 0;
        try {
            DataProvider.getInstance().Open();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        String query = "SELECT MAX(ID) FROM TACGIA";

        ResultSet res = DataProvider.getInstance().ExecuteReader(query);

        try {
            if (res.next()) {
                maxID = res.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            DataProvider.getInstance().Close();
        } catch (SQLException ex) {
            Logger.getLogger(tacGiaProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return maxID;
    }
}
